package com.patterns.chainOfResponsibility.commonChain;

/**
 * Created by Xiao Yiyun on 2017/4/28.
 *抽象处理者类
 */
public abstract class Handler {
    //下一个处理者
    private Handler nextHandler = null;
    //处理者对请求做出处理
    //模板方法模式中的模板方法
    public final Response handleMessage(Request request) {
        Response response = null;
        //判断是否是当前对象的处理级别
        if (this.getHandlerLevel().equals(request.getRequestLevel())) {
            response = this.echo(request);
        } else {
            if (this.nextHandler != null) {
                response = this.nextHandler.handleMessage(request);
            } else {
                //没有适当的处理者，业务自行处理
            }
        }
        return response;
    }
    //设置下一个处理者
    public void setNextHandler(Handler handler) {
        this.nextHandler = handler;
    }
    //模板方法模式中的基本方法
    //得到处理者的处理级别
    protected abstract Level getHandlerLevel();
    //实现处理业务
    protected abstract Response echo(Request request);
}
